package com.rongma.waterdrop.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.rongma.waterdrop.utils.LogUtils;

/**
 * Created by RongMa on 16/5/27.
 */
public class TextProgressBar extends ProgressBar
{
    private String text;
    private Paint paint;
    private boolean isSetText;

    public TextProgressBar(Context context) {
        super(context);
        initTextPoint();
    }

    public TextProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTextPoint();
    }

    public TextProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTextPoint();
    }

    //初始化画笔,设置颜色为白色
    private void initTextPoint() {
        this.paint = new Paint();
        this.paint.setColor(Color.WHITE);
        isSetText = false;
    }

    //绘制
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        if(paint != null && !TextUtils.isEmpty(text)) {
            this.paint.getTextBounds(text, 0, this.text.length(), rect); //返回字符串占据的矩形区域大小
            this.paint.setTextSize(32f);
            int x = getWidth() - rect.width() - 20; //水平 右侧显示
            int y = (getHeight() / 2) + rect.height() / 2; //竖直居中显示
            canvas.drawText(text, x, y, paint); //根据位置 颜色 文字内容 绘制文本
        }
    }

    @Override
    public synchronized void setProgress(int progress) {
        super.setProgress(progress);
    }

    public void setText(String text)
    {
        isSetText = true;
        this.text = text;
    }
}
